package classes.rocks {
	import flash.display.MovieClip;
	import flash.events.*;
	import flash.net.SharedObject;
	import flash.net.SharedObjectFlushStatus;
	import flash.display.Stage;
	import fl.controls.TextInput;
	import flash.text.TextFormat;

	import com.mttang.event.JEvent;
	import com.mttang.utils.PopupWindow;
	import com.mttang.event.CommonEvent;
	import classes.savedata.RockmanData;
	import classes.GameSetting;

	/**
	 * ...
	 * @author fox
	 */
	public class CreateName extends MovieClip {

		private var id:int;
		private var sharedData:Object;
		private var createMan:CreateMan;
		private var _saveData:Object;
		private var createWindow:CreateNameWindow;

		public function CreateName(){
			//取得共享数据
			getSharedData();
		}

		private function getSharedData():void {
			sharedData = SharedObject.getLocal(GameSetting.saveSoData);
			//sharedData.clear();
			//显示数据
			showSaveData();
		}

		private function showSaveData():void {
			if (sharedData.data.rockmanFlag == "dare.mttang.com") {
				for (var i:int = 0; i < sharedData.data.dataRockman.length; i++ ) {
					if (sharedData.data.dataRockman[i] != null) {
						createName_bg["name_txt_" + sharedData.data.dataRockman[i].id].text = sharedData.data.dataRockman[i].user.name + " ------- level " + sharedData.data.dataRockman[i].user.level;
					}else {
						trace("is null!");
					}					
				}
			}else {
				sharedData.data.rockmanFlag == "dare.mttang.com";
				//创建第一个人物
				createInitRockMan(1);
			}			
			//按钮监听事件处理
			addEventListener(MouseEvent.CLICK, createNameClickHandler);			
		}
		
		//创建第一个人物
		private function createInitRockMan(_id:int = 1):void {
			id = _id;
			//第一次创建人物
			createName_bg.visible = false;
			createMan = new CreateMan();
			addChildAt(createMan, numChildren);
			createMan.addEventListener(CommonEvent.CREATE_ROCKMAN_EVENT, createRockManHandler);
		}
		
		private function createRockManHandler(e:JEvent):void {
			createMan.removeEventListener(CommonEvent.CREATE_ROCKMAN_EVENT, createRockManHandler);
			removeChild(createMan);
			createMan = null;
			if (_saveData) {
				_saveData = null;
			}
			_saveData = new Object();
			_saveData.user = new Object();
			_saveData.user.manid = e.info.manid;			
			//创建名字
			createNameWindow(id);
		}
		
		private function createNameWindow(_id:int):void {
			id = _id;
			PopupWindow.init(stage);
			createWindow = new CreateNameWindow();
			//createWindow.input_txt.alwaysShowSelection = true;
			var format:TextFormat = new TextFormat();
			format.size = 20;
			createWindow.input_txt.setStyle("textFormat",format);
			PopupWindow.show(createWindow);
			createWindow.btn_no.addEventListener(MouseEvent.CLICK, btnnoClickHandler);
			createWindow.btn_yes.addEventListener(MouseEvent.CLICK,btnyesClickHandler);
		}
		
		private function btnnoClickHandler(e:MouseEvent):void {
			createName_bg.visible = true;
			createWindow.btn_no.removeEventListener(MouseEvent.CLICK, btnnoClickHandler);
			createWindow.btn_yes.removeEventListener(MouseEvent.CLICK,btnyesClickHandler);
			PopupWindow.close();
		}
		
		private function btnyesClickHandler(e:MouseEvent):void {
			createName_bg.visible = true;
			createWindow.btn_no.removeEventListener(MouseEvent.CLICK, btnnoClickHandler);
			createWindow.btn_yes.removeEventListener(MouseEvent.CLICK,btnyesClickHandler);
			PopupWindow.close();			
			if (createWindow.input_txt.text != "") {
				saveRockManData(id, createWindow.input_txt.text);
			}
		}
		
		private function saveRockManData(_id:int, _str:String):void {
			_saveData.id = _id;
			//user
			_saveData.user.name = _str;
			_saveData.user.exp = GameSetting.initExp;
			_saveData.user.level = GameSetting.initLevel;
			_saveData.user.money = GameSetting.initMoney;
			//pet
			var _petArr = new Array();
			_saveData.pet = _petArr;
			var _pet1:Object = new Object();
			_pet1.name = GameSetting.PETNAME[int(Math.random() * GameSetting.PETNAME.length)];
			_pet1.petid = 2;
			_pet1.level = GameSetting.initPetLevel;
			_pet1.skillcount = 0;
			_pet1.status = GameSetting.initPetStatus;
			_pet1.anti = 5;
			var _skillsArr:Array=[[0, 5], [0, 3], [0, 5], [0, 4], [0, 5], [0, 3], [0, 1]];
			_pet1.skills = _skillsArr;
			_saveData.pet.push(_pet1);
			
			var _pet2:Object = new Object();
			_pet2.name = GameSetting.PETNAME[int(Math.random() * GameSetting.PETNAME.length)];
			_pet2.petid = 3;
			_pet2.level = GameSetting.initPetLevel;
			_pet2.skillcount = 0;
			_pet2.status = GameSetting.initPetStatus;
			_pet2.anti = 5;
			_pet2.skills = _skillsArr;
			_saveData.pet.push(_pet2);			
			//bag
			var _bagArr = new Array();
			_saveData.bag = _bagArr;
			//save data
			RockmanData.saveData(_saveData);	
			dispatch(_id,_saveData.user.name,_saveData.user.manid);
		}

		private function dispatch(_id:int,_userName:String,_manid:String):void {
			id = _id;
			var _obj:Object = new Object();
			_obj.id = _id;
			var _userObj = new Object();
			_obj.user = _userObj;
			_obj.user.name = _userName;
			_obj.user.manid = _manid;
			dispatchEvent(new JEvent(CommonEvent.CREATE_NAME_EVENT, _obj));
			removeEventListener(MouseEvent.CLICK, createNameClickHandler);
		}

		private function createNameClickHandler(e:MouseEvent):void {
			var tmpStr = (e.target.name).split("btn_")[1];
			switch (tmpStr){
				case "play_1":
					startGame(1);
					break;
				case "play_2":
					startGame(2);
					break;
				case "play_3":
					startGame(3);
					break;
				case "create_1":
					createInitRockMan(1);
					break;
				case "create_2":
					createInitRockMan(2);
					break;
				case "create_3":
					createInitRockMan(3);
					break;
				case "back":
					dispatchEvent(new Event(CommonEvent.BACK_EVENT));
					break;	
				default:
					break;
			}
		}

		private function startGame(_id:int):void {
			id = _id;
			if (createName_bg["name_txt_" + _id].text == ""){
				createInitRockMan(_id);
			} else {
				if (sharedData.data.dataRockman[int(_id-1)]!=null) {
					var _tmpUserName:String = sharedData.data.dataRockman[int(_id - 1)].user.name;
					var _tmpGameId:String = sharedData.data.dataRockman[int(_id-1)].user.manid;
					dispatch(_id,_tmpUserName,_tmpGameId);
				}
			}
		}
	/******************************************************************************/
	}
}